/*
Kakine
ActionScript Framework
Copyright © 2011, PUNY, LLC.

This file is part of KAKINE.

KAKINE is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

KAKINE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with KAKINE.  If not, see <http://www.gnu.org/licenses/>.
*/ 
package com.puny.mvc.views.singleviews
{
	import com.puny.mvc.views.ButtonView;
	
	import flash.display.MovieClip;
	import flash.geom.Rectangle;
	
	/**
	 * $Revision: 706 $<br/>
	 * RichView<br/>
	 * This view can have the only one asset. This class accepts time line animation
	 * which name is <code>open</code> or <code>close</code>.
	 */
	public class RichView extends ButtonView
	{
		/**
		 * A frame label for an animation when the button shows up.
		 * After this animation, the current label should be <code>OUT</code> if it exists.
		 * The label name is case-sensitive.
		 */
		public static const OPEN:String = "open";
		/**
		 * A frame label for an animation before the button closes.
		 * After this animation, the button will be removed.
		 * The label name is case-sensitive.
		 */
		public static const CLOSE:String = "close";
		protected var _mc:MovieClip;
		private var _name:String;
//		private var _width:Object = new Object();
		protected var _rect:Object = new Object();
		private var _final:Object = new Object();
		public function RichView(src:Object,exportedName:String)
		{
			_name = exportedName;
			super(src);
		}

		public function get exportedName():String {
			return _name;
		}
		
		override protected function setup():void {
			trace(_name);
			_mc = getMovieClip(_name);
			
			//Make registration point ignored.
			var rect:Rectangle = _mc.getBounds(this);
			_mc.x = -rect.x;
			_mc.y = -rect.y;
			
			analyzeEachFrame();
		}
		
		private function analyzeEachFrame():void {
			var current:int = _mc.currentFrame;
			var rect:Rectangle;
			for(var i:int = 0;i < _mc.totalFrames;i++) {
				_mc.gotoAndStop(i);
				_final[_mc.currentLabel] = i;
				rect = _mc.getBounds(this);
//				trace(i,",",rect.x,",",rect.y,",",rect.right,",",rect.bottom);
				if (_rect["whole"] == null) {
					_rect["whole"] = rect;
//				if (_rect[_mc.currentLabel] == null) {
//					_rect[_mc.currentLabel] = rect;
				} else {
					_rect["whole"].x = _rect["whole"].x < rect.x ? _rect["whole"].x : rect.x;
					_rect["whole"].y = _rect["whole"].y < rect.y ? _rect["whole"].y : rect.y;
					_rect["whole"].right = _rect["whole"].right > rect.right ? _rect["whole"].right : rect.right;
					_rect["whole"].bottom = _rect["whole"].bottom > rect.bottom ? _rect["whole"].bottom : rect.bottom;
//					_rect[_mc.currentLabel].x = _rect[_mc.currentLabel].x < rect.x ? _rect[_mc.currentLabel].x : rect.x;
//					_rect[_mc.currentLabel].y = _rect[_mc.currentLabel].y < rect.y ? _rect[_mc.currentLabel].y : rect.y;
//					_rect[_mc.currentLabel].width = _rect[_mc.currentLabel].width > rect.width ? _rect[_mc.currentLabel].width : rect.width;
//					_rect[_mc.currentLabel].height = _rect[_mc.currentLabel].height > rect.height ? _rect[_mc.currentLabel].height : rect.height;
				}
//				if (_width.hasOwnProperty(_mc.currentLabel)) {
//					var w:Number = _width[_mc.currentLabel] 
//					_width[_mc.currentLabel] = w > _mc.width ? w : _mc.width;
//				} else {
//					_width[_mc.currentLabel] = _mc.width;
//				}
//				trace(" label:", _mc.currentLabel, ":", _width[_mc.currentLabel]);
			}
//			for(var label:* in _rect) {
//				trace(label,_rect[label]);
//			}
			_mc.gotoAndPlay(current);
		}
		
		protected function get maxRect():Rectangle {
			return _rect["whole"];
//			return _rect[_mc.currentLabel];
		}
		
		public function get animation_width():Number {
			return _rect["whole"].width;
//			return _rect[_mc.currentLabel].width;
		}
		
		public function isFinalOf(label:Object):Boolean {
//			trace(_mc.currentFrame,_mc.currentLabel);
			if(_mc == null) {
				return true;
			}
			if (label is String) {
				return _final[label] == _mc.currentFrame;
			}
			for each(var str:String in label) {
				if (_final[str] == _mc.currentFrame) {
					return true;
				}
			}
			return false;
		}

		/**
		 * Play <code>OPEN</code> animation
		 */
		override public function beforeOpen():void {
			gotoAndPlay(OPEN);
		}
		
		/**
		 * Watch opening animation
		 */
		override public function procedureOpen():Boolean {
			return _mc.currentLabel == OPEN;
		}

		/**
		 * play closing animation.
		 */		
		override public function beforeClose():void {
			gotoAndPlay(CLOSE);
		}

		/**
		 * watch closing animation
		 */
		override public function procedureClose():Boolean {
//			trace("---------------",_mc.currentFrame,_mc.currentLabel,_mc.totalFrames);
			return _mc != null && (_mc.currentLabel == CLOSE && _mc.currentFrame != _mc.totalFrames);
		}
		
		override public function afterClose():void {
			super.afterClose();
			_mc = null;	
		}
		
		public function get movieClip():MovieClip {
			return _mc;
		}
		public function gotoAndPlay(label:String):Boolean {
			if (_mc==null||!_mc[label]) {
				return false;
			}
			_mc.gotoAndPlay(label);
			return true;
		}
		
		public function gotoAndStop(label:String):Boolean {
			if (_mc==null||!_mc[label]) {
				return false;
			}
			_mc.gotoAndStop(label);
			return true; 
		}
		
	}
}